
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: [
      { tid: '1', tname: "经典", isSelected: false },
      { tid: '2', tname: "本草", isSelected: false },
      { tid: '3', tname: "针灸", isSelected: false },
      { tid: '4', tname: "养生", isSelected: false },
      { tid: '5', tname: "脉学", isSelected: false },
      { tid: '6', tname: "诊断", isSelected: false },
      { tid: '7', tname: "方药", isSelected: false },
      { tid: '8', tname: "内科", isSelected: false },
      { tid: '9', tname: "外科", isSelected: false },
      { tid: '10', tname: "妇科", isSelected: false },
      { tid: '11', tname: "儿科", isSelected: false },
      { tid: '12', tname: "骨伤", isSelected: false }
    ],
    jdbooks: [
      { bid: '1', bname: "伤寒论", bzuozhe: "张仲景", bookimage: "/assets/book/jingdian/1.jpg", bookurl: "../article/article" ,tid:'1'},
      { bid: '2', bname: "金匮要略", bzuozhe: "张仲景", bookimage: "/assets/book/jingdian/2.jpg" ,tid:'1'},
      { bid: '3', bname: "黄帝内经素问", bzuozhe: "佚名", bookimage: "/assets/book/jingdian/3.jpg",tid:'1' },
      { bid: '4', bname: "黄帝内经灵枢", bzuozhe: "佚名", bookimage: "/assets/book/jingdian/4.jpg" ,tid:'1'}
    ],
    bcbooks: [
      { bid: '1', bname: "本草纲目", bzuozhe: "李时珍", bookimage: "/assets/book/bencao/1.jpg",tid:'2' },
      { bid: '2', bname: "神农本草经", bzuozhe: "佚名", bookimage: "/assets/book/bencao/2.jpg" ,tid:'2'}

    ],
    books: [],
    bookshelf: [] // 书架数组
  },

  getTypeId: function (event) {
    const { tid } = event.currentTarget.dataset; // 获取被点击元素的 tid  
    const types = [...this.data.types]; //...复制 types 数组以避免直接修改数据  

    // 更新选中状态  
    types.forEach(item => {
      item.isSelected = (item.tid === tid);
    });

    // 根据 tid 设置对应的书籍数据  
    let books;
    if (tid === '1') {
      books = this.data.jdbooks;
    } else if (tid === '2') {
      books = this.data.bcbooks;
    } else {
      books = []; // 或者其他默认数据  
    }

    this.setData({
      types: types, // 更新选中状态  
      books: books  // 更新书籍数据  
    });
  },
  // 1.搜索框处理
  onSearchInput(e) {
    this.setData({
      searchValue: e.detail.value
    });
  },

  onSearch() {
    console.log('搜索内容：', this.data.searchValue);
  },
  onInput: function (e) {
    const searchValue = e.detail.value;
    this.setData({
      searchValue: searchValue
    });
    this.filterBooks(searchValue); // 调用筛选函数，传递当前输入的关键词
  },

  // 点击搜索按钮时触发的函数，进行书籍筛选
  onSearch: function () {
    const searchValue = this.data.searchValue;
    this.filterBooks(searchValue);
  },

  // 辅助函数，用于根据关键词对书籍进行模糊筛选
  filterBooks: function (searchValue) {
    let allBooks = [...this.data.jdbooks,...this.data.bcbooks]; // 合并所有书籍数据，可根据实际更多分类情况调整
    if (!searchValue) {
      // 如果搜索关键词为空，显示全部书籍
      this.setData({
        books: allBooks
      });
      return;
    }
    // 进行模糊搜索筛选，将关键词转为小写后检查书籍名称是否包含该关键词（不区分大小写）
    const filteredBooks = allBooks.filter(book => {
      return book.bname.toLowerCase().includes(searchValue.toLowerCase());
    });
    this.setData({
      books: filteredBooks // 更新展示的书籍列表为筛选后的结果
    });
  },

  successadd: function (event) {
    // 从事件中获取当前书籍的 bid  
    const bid = event.currentTarget.dataset.bid;

    // 从 books 数组中找到对应的书籍对象  
    const bookToAdd = this.data.books.find(book => book.bid === bid);

    // 检查书籍是否已存在于书架中 
    const isBookInShelf = this.data.bookshelf.some(book => book.bid === bid);
    if (isBookInShelf) {
      wx.showToast({
        title: '书籍已在书架中',
        icon: 'none'
      });
      return;
    }

    // 将书籍添加到书架数组  
    const newBookshelf = [...this.data.bookshelf, bookToAdd];

    // 更新页面的数据  
    this.setData({
      bookshelf: newBookshelf
    });
    const app = getApp();
    app.globalData.bookshelf = this.data.bookshelf;
    // 如果需要，可以显示一个成功提示或进行其他操作  
    wx.showToast({
      title: '添加成功',
      icon: 'success'
    });


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 模拟点击经典项
    this.getTypeId({ currentTarget: { dataset: { tid: '1' } } });
  },
  goToBookDetail: function (event) {
    console.log("完整信息：",event.currentTarget)
    console.log("dataset完整信息：",event.currentTarget.dataset)
    const bid = event.currentTarget.dataset.bid;
    const tid = event.currentTarget.dataset.tid;
    console.log("现在tid的值：",tid);
    console.log("现在bid的值：",bid);
    wx.navigateTo({
      url: `/pages/bookdetail/bookdetail?bid=${bid}&tid=${tid}`,
      fail: function (err) {
        console.error('跳转失败：', err);
        // 可以添加提示用户跳转失败的交互，比如弹出提示框等
        wx.showToast({
          title: '跳转失败，请稍后再试',
          icon: 'none'
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})